package com.controller.contractor;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.DateUtils;
import com.common.http.HttpResult;
import com.enums.FactoryEnum;
import com.service.contractor.VisaService;
import com.model.contractor.VisaModel;

/**
 * 
 */
@Controller("visaController")
@RequestMapping(value = "/api")
public class VisaController {
	static final Logger logger = LoggerFactory.getLogger(VisaController.class);

	@Autowired
	VisaService visaService;

	/**
	 * 查询列表
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "con/visa/selectList", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult selectList(@RequestParam Map<String, Object> map) {
		return visaService.selectList(map);
	}

	@RequestMapping(value = "con/visa/selectListXY", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult selectListXY(@RequestParam Map<String, Object> map) {
		map.put("visFactoryCode", FactoryEnum.xy.getValue());
		return visaService.selectListXY(map);
	}

	@RequestMapping(value = "con/visa/selectListSP", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult selectListSP(@RequestParam Map<String, Object> map) {
		map.put("visFactoryCode", FactoryEnum.sp.getValue());
		return visaService.selectListSP(map);
	}

	/**
	 * 查询详情
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "con/visa/selectInfor", method = { RequestMethod.GET })
	@ResponseBody
	public HttpResult selectInfor(@RequestParam String visId) {
		return visaService.selectInfor(visId);
	}

	/**
	 * 保存询价
	 * 
	 * @param
	 * @return
	 */
	@RequestMapping(value = "con/visa/saveXY", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult saveXY(@RequestBody VisaModel visaModel) {
		return visaService.saveXY(visaModel);
	}

	@RequestMapping(value = "con/visa/saveSP", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult saveSP(@RequestBody VisaModel visaModel) {
		return visaService.saveSP(visaModel);
	}

	@RequestMapping(value = "con/visa/printXY", method = { RequestMethod.GET })
	@ResponseBody
	public HttpResult printXY(@RequestParam String visId) {
		return visaService.printXY(visId);
	}

	@RequestMapping(value = "con/visa/printSP", method = { RequestMethod.GET })
	@ResponseBody
	public HttpResult printSP(@RequestParam String visId) {
		return visaService.printSP(visId);
	}

	@RequestMapping(value = "con/visa/back", method = { RequestMethod.GET })
	@ResponseBody
	public HttpResult back(@RequestParam String visId) {
		return visaService.back(visId);
	}

	@RequestMapping(value = "con/visa/commitXY", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult commitXY(@RequestBody VisaModel visaModel) {
		return visaService.commitXY(visaModel);
	}

	@RequestMapping(value = "con/visa/commitSP", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult commit(@RequestBody VisaModel visaModel) {
		return visaService.commitSP(visaModel);
	}

	/**
	 * 删除询价
	 * 
	 * @param
	 * @return
	 */
	@RequestMapping(value = "con/visa/delete", method = { RequestMethod.DELETE })
	@ResponseBody
	public HttpResult delete(@RequestParam String visId) {
		return visaService.delete(visId);
	}

	/**
	 * 导出
	 * 
	 * @param map
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "con/visa/export")
	public void export(@RequestParam Map<String, Object> map, HttpServletResponse response) throws IOException {
		response.setContentType("application/vnd.ms-excel");
		response.setCharacterEncoding("utf-8");
		String fileName = URLEncoder.encode("采购订单-" + DateUtils.getCurrentDate(), "UTF-8");
		response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
		visaService.export(map, response);

	}

	@RequestMapping(value = "con/visa/exportXY")
	public void exportXY(@RequestParam Map<String, Object> map, HttpServletResponse response) throws IOException {
		response.setContentType("application/vnd.ms-excel");
		response.setCharacterEncoding("utf-8");
		String fileName = URLEncoder.encode("采购订单-" + DateUtils.getCurrentDate(), "UTF-8");
		response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
		map.put("visFactoryCode", FactoryEnum.xy.getValue());
		visaService.exportXY(map, response);

	}

	@RequestMapping(value = "con/visa/exportSP")
	public void exportSP(@RequestParam Map<String, Object> map, HttpServletResponse response) throws IOException {
		response.setContentType("application/vnd.ms-excel");
		response.setCharacterEncoding("utf-8");
		String fileName = URLEncoder.encode("采购订单-" + DateUtils.getCurrentDate(), "UTF-8");
		response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
		map.put("visFactoryCode", FactoryEnum.sp.getValue());
		visaService.exportSP(map, response);

	}
}
